package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Banner;
import com.itheima.domain.BannerExample;
import com.itheima.mapper.BannerMapper;
import com.itheima.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {
    @Autowired
    private BannerMapper bannerMapper;
    @Override
    public PageInfo<Banner> findByPage(Integer pageNum, Integer pageSize,String keyword) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //查找List
        BannerExample bannerExample = new BannerExample();
        BannerExample.Criteria criteria = bannerExample.createCriteria();
        if (keyword!=null && keyword.length()>0){
            criteria.andTitleLike("%"+keyword+"%");
        }
        List<Banner> list = bannerMapper.selectByExample(bannerExample);
        return new PageInfo<Banner>(list);
    }

    @Override
    public void save(Banner banner) {
        if (banner.getId()!=null){
            //则为修改
            bannerMapper.updateByPrimaryKeySelective(banner);
        }else {
            bannerMapper.insert(banner);
        }
    }

    @Override
    public Banner findById(Integer id) {
        return bannerMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(Integer id) {
        bannerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteIds(Integer[] ids) {
        bannerMapper.deleteIds(ids);
    }

    @Override
    public List<Banner> findBannerList() {
        //查询前五条
        PageHelper.startPage(1,5);

        //并按照序号排序
        BannerExample bannerExample = new BannerExample();
        bannerExample.setOrderByClause("order_no asc");
        return bannerMapper.selectByExample(bannerExample);
    }
}
